package com.carkeeper.gateway.service;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.http.HttpCookie;
import org.springframework.http.server.reactive.ServerHttpRequest;
import org.springframework.stereotype.Service;
import org.springframework.util.MultiValueMap;

@Service
public class AuthService {


    @Autowired
    private StringRedisTemplate stringRedisTemplate;
    /**
     * 获取cookie中的令牌id,查看cookie中jti是否存在
     *
     * @param request
     * @return
     */
    public String getJtiFromCookie(ServerHttpRequest request) {

        MultiValueMap<String, HttpCookie> cookies = request.getCookies();
        HttpCookie jti = cookies.getFirst("uid");
        if (jti != null) {

            return jti.getValue();
        }


        return null;
    }


    /**
     * 查看redis中的token是否过期或者存在,从redis中取出token令牌
     * @param jti
     * @return
     */
    public String getTokenFromRedis(String jti) {

        String token = stringRedisTemplate.boundValueOps(jti).get();

        return token;
    }
}
